﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alma.Models;
using AlmaDatabase;

namespace Alma.Controllers
{
    public class CartController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Show()
        {
            CartModel newComp = new CartModel();
            if (Session["UserId"] == null)
                return RedirectToAction("Connect", "User");
            using (var context = new AlmaDBEntities())
            {
                long usrID = (long)Session["UserID"];
                /*product*/
                var list = from product in context.Products
                           join jproduct in context.Cart on product.ID equals jproduct.ID_Product
                           where jproduct.ID_User == usrID
                           orderby product.ID_Shop
                           select product;
                foreach (var pro in list)
                {
                    ProductDetailsModel productModel = new ProductDetailsModel(pro);
                    var cartProd = from cart in context.Cart
                                   where cart.ID_Product == pro.ID
                                   select cart;
                    productModel.Quantity = cartProd.First().Quantity;
                    newComp.productList.Add(productModel);
                }
                /*lists*/
                
                var lists = from usrlist in context.T_List
                            where usrlist.ID_User == usrID
                            select usrlist;
               // List<string> dropList = new List<string>();
                foreach (var ulist in lists)
                {
                 //   dropList.Add(ulist.List_Name);
                    newComp.publicLists.Add(new ListModel(ulist.ID_List, ulist.ID_User, ulist.List_Name));
                }
                //ViewData["drop_list"] = new SelectList(dropList);

            }
            return View(newComp);
        }

        public ActionResult Print()
        {
            CartModel newComp = new CartModel();
            if (Session["UserId"] == null)
                return RedirectToAction("Connect", "User");
            using (var context = new AlmaDBEntities())
            {
                var list = from product in context.Products
                           join jproduct in context.Cart on product.ID equals jproduct.ID_Product
                           orderby product.ID_Shop
                           select product;
                foreach (var pro in list)
                {
                    ProductDetailsModel productModel = new ProductDetailsModel(pro);
                    var cartProd = from cart in context.Cart
                                   where cart.ID_Product == pro.ID
                                   select cart;
                    productModel.Quantity = cartProd.First().Quantity;
                    newComp.productList.Add(productModel);
                }
            }
            return View(newComp);
        }

        public ActionResult Add(int Id)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    Cart c = new Cart();
                    c.ID_User = (long)Session["UserId"];
                    c.ID_Product = Id;
                    c.Date = DateTime.Now;
                    c.Quantity = 1;
                    // context.Cart.AddObject(Cart.CreateCart((long)Session["UserId"], Id, DateTime.Now, 1));
                    context.Cart.AddObject(c);
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "Cart");
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "Cart");
            }
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    var userId = (long)Session["UserId"];
                    context.Cart.DeleteObject(context.Cart.Where(x => x.ID_Product == Id && x.ID_User == userId).Single());
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "Cart");
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "Cart");
            }
        }

        public ActionResult Free()
        {
            using (var context = new AlmaDBEntities())
            {
                var userId = (long)Session["UserId"];
                var list = from cart in context.Cart
                           where cart.ID_User == userId
                           select cart;
                foreach (var product in list)
                    context.Cart.DeleteObject(product);
                context.SaveChanges();
            }
            return RedirectToAction("Show", "Cart");
        }

        public ActionResult More(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var userId = (long)Session["UserId"];
                var cartProd = from cart in context.Cart
                               where cart.ID_User == userId
                               where cart.ID_Product == id
                               select cart;
                if (cartProd.First().Quantity < 10000)
                    cartProd.First().Quantity++;
                context.SaveChanges();
            }
            return RedirectToAction("Show", "Cart");
        }

        public ActionResult Less(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var userId = (long)Session["UserId"];
                var cartProd = from cart in context.Cart
                               where cart.ID_User == userId
                               where cart.ID_Product == id
                               select cart;
                if (cartProd.First().Quantity > 0)
                    cartProd.First().Quantity--;
                context.SaveChanges();
            }
            return RedirectToAction("Show", "Cart");
        }
    }
}
